package com.motion.androidlearnning.widget;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.motion.androidlearnning.R;

import java.util.ArrayList;
import java.util.List;

@Route(path = "/widget/expandrecyclerview")
public class ExpandRecyclerViewActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.layout_widget_expandrecyclerview);
        mRecyclerView = this.findViewById(R.id.recyclerview);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(manager);
        initData();
    }


    private void initData() {
        List<GroupType> content = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            GroupType type = new GroupType(i + "");
            content.add(type);
        }
        GroupType type = new GroupType(10 + "");
        for (int i = 0; i < 10; i++) {
            type.addChild(new ItemType("-------:" + i));
        }
        content.add(type);
        for (int i = 0; i < 10; i++) {
            GroupType type2 = new GroupType(i + "");
            content.add(type2);
        }
        mRecyclerView.setAdapter(new ExpandAdapter(this, content));
    }
}

class ExpandAdapter extends RecyclerView.Adapter<MyViewHolder> {

    private List<GroupType> mContent;
    private LayoutInflater layoutInflater;
    private List<Type> mTypes = new ArrayList<>();

    public ExpandAdapter(Context context, List<GroupType> content) {
        layoutInflater = LayoutInflater.from(context);
        mContent = content;
    }

    @Override
    public int getItemViewType(int position) {
        return mTypes.get(position).getType();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == Type.TYPE_GROUP) {
            View view = layoutInflater.inflate(R.layout.layout_widget_expand_group, parent, false);
            return new MyViewHolder(view, viewType);
        }
        if (viewType == Type.TYPE_ITEM) {
            View view = layoutInflater.inflate(R.layout.layout_widget_expand_item, parent, false);
            return new MyViewHolder(view, viewType);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        if (holder.myType == Type.TYPE_GROUP) {
            holder.tvGroup.setText(mTypes.get(position).getTitle());
            holder.tvGroup.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    GroupType type = (GroupType) mTypes.get(position);
                    android.util.Log.e("wangx", "---position:" + position);
                    if (!type.isExpanded()) {
                        type.setExpanded(true);
                    } else {
                        type.setExpanded(false);
                    }
                    notifyDataSetChanged();
                }
            });
        }
        if (holder.myType == Type.TYPE_ITEM) {
            holder.tvItem.setText(mTypes.get(position).getTitle());
        }
    }

    private void updateTypes() {
        mTypes.clear();
        for (GroupType t : mContent) {
            mTypes.add(t);
            if (t.isExpanded()) {
                mTypes.addAll(t.getmChildren());
            }
        }
    }

    @Override
    public int getItemCount() {
        updateTypes();
        return mTypes.size();
    }
}

class MyViewHolder extends RecyclerView.ViewHolder {
    public int myType;
    public TextView tvGroup;
    public TextView tvItem;

    public MyViewHolder(@NonNull View itemView, int type) {
        super(itemView);
        myType = type;
        if (type == Type.TYPE_GROUP) {
            tvGroup = itemView.findViewById(R.id.tv_group);
        }
        if (type == Type.TYPE_ITEM) {
            tvItem = itemView.findViewById(R.id.tv_item);
        }
    }
}

class ItemType implements Type {
    private String itemTitle;

    public ItemType(String title) {
        itemTitle = title;
    }

    @Override
    public int getType() {
        return Type.TYPE_ITEM;
    }

    @Override
    public String getTitle() {
        return itemTitle;
    }
}


class GroupType implements Type {
    private List<ItemType> mChildren = new ArrayList<>();
    private String groupTitle;
    private boolean isExpanded;

    public GroupType(String groupTitle) {
        this.groupTitle = groupTitle;
    }

    public boolean isExpanded() {
        return isExpanded;
    }

    public void setExpanded(boolean expanded) {
        isExpanded = expanded;
    }

    public void addChild(ItemType childType) {
        mChildren.add(childType);
    }

    @Override
    public String getTitle() {
        return groupTitle;
    }

    public List<ItemType> getmChildren() {
        return mChildren;

    }

    @Override
    public int getType() {
        return Type.TYPE_GROUP;
    }
}

interface Type {
    public static final int TYPE_ITEM = 0;
    public static final int TYPE_GROUP = 1;

    int getType();

    String getTitle();
}
